package com.easidea.tim.flex.remote.inventory
{
	import com.easidea.tim.flex.Env;
	import com.easidea.tim.flex.bean.Page;
	import com.easidea.tim.flex.bean.SysLocator;
	import com.easidea.tim.flex.bean.inventory.CheckInOut;
	import com.easidea.tim.flex.bean.purchase.PurchaseOrder;
	import com.easidea.tim.flex.bean.saleorder.SaleOrder;
	import com.easidea.tim.flex.event.CustomerEvent;
	import com.easidea.tim.flex.remote.BaseDao;
	import com.easidea.tim.flex.util.AppAlert;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	import net.digitalprimates.persistence.hibernate.rpc.HibernateRemoteObject;

	public class CheckInOutDao extends BaseDao
	{
		private var _service:HibernateRemoteObject = new HibernateRemoteObject();
		private static var _instance:CheckInOutDao;
		
		[Bindable] public var stuffList : ArrayCollection; // 员工信息 autocomplete 过滤数组 
		
		/**
		 * Singleton
		 */
		public static function getInstance() : CheckInOutDao {
			if ( _instance == null ){
				_instance = new CheckInOutDao();
			}
			return _instance;
		}
		
		
		function CheckInOutDao(){
			_service.channelSet = this.buildChannelSet();
			_service.destination = "checkInOutService";
			_service.concurrency = "last";
			_service.makeObjectsBindable = true;
			_service.showBusyCursor = true;
		}
		
		/**
		 *  出入库记录查询
		 *  @param operator 操作人
		 *  @param billFlowNum: 单据编号，是指与出入有关的所有单据的流程编号
		 *  @param billType: 关联单据类型，如销售订单等
		 *  @param searchStartDate: 搜索开始时间
		 *  @param searchEndDate: 搜索结束时间
		 *  @param state: 状态
		 *  @param productName: 品名
		 *  @param colorCode: 色号
		 */
		public function getCheckInOutList(cioType:int,operator:String, billFlowNum:String, billType:int, searchStartDate:Date, searchEndDate:Date, state:String, productName:String, colorCode:String, page:Page) : void{
			_service.addEventListener(ResultEvent.RESULT,showCheckInOutList);
			_service.addEventListener(FaultEvent.FAULT, _Fault);
			_service.queryCheckInOutList(cioType,operator,billFlowNum,billType,searchStartDate,searchEndDate,state, productName, colorCode, page);
		}
		
		
		private function showCheckInOutList(e:ResultEvent):void{
			_service.removeEventListener(ResultEvent.RESULT,showCheckInOutList);
			if(e.result != null){
				var page:Page = e.result as Page;
				var loginuser:String = SysLocator.getInstance().loggedUser;
				if(page.list != null && page.list.length > 0){
					var rtnList:ArrayCollection = new  ArrayCollection();
					for each (var objArray:Array in page.list ){
						var value:Object = {cioId:objArray[0],state:objArray[1], productName:objArray[3],colorCode:objArray[4],isSample:objArray[5], creatDate:objArray[6], piNum:objArray[7], meterNum: objArray[8], creator:objArray[9] ,login:loginuser};
						rtnList.addItem(value);
					}
					page.list = rtnList;
				}
				Env.dispatchEvent(new CustomerEvent("getCheckInOutListSuccess",page));
			}
		}
		
		/**
		 * 保存出入库记录
		 * @param checkInOut
		 */
		public function saveCheckInOut(checkInOut:CheckInOut) : void {
			_service.addEventListener(ResultEvent.RESULT,saveCheckInOutProcessed);
			_service.addEventListener(FaultEvent.FAULT, _Fault);
			_service.checkInOutProduct(checkInOut);
		}
		
		private function saveCheckInOutProcessed(e:ResultEvent) : void {
			_service.removeEventListener(ResultEvent.RESULT,saveCheckInOutProcessed);
			if(e.result != null) {
				Env.dispatchEvent(new CustomerEvent("saveCheckInOutComplete",e.result as int));
			}
		}
		
		/**
		 * 提取单一出入库数据
		 */
		public function getCheckInOutById(id:Number):void {
			_service.addEventListener(ResultEvent.RESULT,getCheckInOutByIdProcessed);
			_service.addEventListener(FaultEvent.FAULT, _Fault);
			_service.getCheckInOutById(id);
		}
		
		private function getCheckInOutByIdProcessed(e:ResultEvent):void{
			_service.removeEventListener(ResultEvent.RESULT,getCheckInOutByIdProcessed);
			if(e.result != null){
				var cio:CheckInOut = new CheckInOut;
				cio.createTime = e.result.createTime;
				cio.creator = e.result.creator;
				cio.deleted = e.result.deleted;
				cio.description = e.result.description;
				cio.direction = e.result.direction;
				cio.id = e.result.id;
				cio.items = e.result.items;
				cio.meterNumSummary = e.result.meterNumSummary;
				cio.modifier = e.result.modifier;
				cio.modifyTime = e.result.modifyTime;
				cio.piNumSummary = e.result.piNumSummary;
				cio.type = e.result.type;
				if(e.result.purchaseOrder != null){
					var po:PurchaseOrder = new PurchaseOrder();
					po.flowNo = e.result.purchaseOrder.flowNo;
					cio.purchaseOrder = po;
				}
				if(e.result.salesOrder != null){
					var so:SaleOrder = new SaleOrder();
					so.flowNo = e.result.saleOrder.flowNo;
					cio.saleOrder = so;
				}
				Env.dispatchEvent(new CustomerEvent("getCheckInOutByIdSuccess",cio));
			}
		}
		
		/***
		 * 根据销售订单完成出库
		 * 
		 */
		public function checkOutBySaleOrder(id:int, creator:String,orderType:String):void{
			_service.addEventListener(ResultEvent.RESULT,checkOutBySaleOrderResult);
			_service.addEventListener(FaultEvent.FAULT, _Fault);
			if(orderType == "0" || orderType == "1"){
				_service.checkOutBySaleOrder(id,creator);
			}else{
				_service.checkInBySalesReturn(id,creator);
			}
		}
		
		private function checkOutBySaleOrderResult(e:ResultEvent):void{
			_service.removeEventListener(ResultEvent.RESULT,checkOutBySaleOrderResult);
			Env.dispatchEvent(new CustomerEvent("checkOutBySaleOrderSuccess",e.result));
		}
		
		
		/***
		 * 根据采购退货单出库
		 * 
		 */
		public function checkOutByPurchaseReturn(id:int, creator:String):void{
			_service.addEventListener(ResultEvent.RESULT,checkOutByPurchaseReturnResult);
			_service.addEventListener(FaultEvent.FAULT, _Fault);
			_service.checkOutByPurchaseReturn(id,creator);
		}
		
		private function checkOutByPurchaseReturnResult(e:ResultEvent):void{
			_service.removeEventListener(ResultEvent.RESULT,checkOutByPurchaseReturnResult);
			Env.dispatchEvent(new CustomerEvent("checkOutByPurchaseReturnSuccess",e.result));
		}
		
		
		public function updatePurchaseStatus(id:int):void{
			_service.addEventListener(ResultEvent.RESULT,updatePurchaseStatusResult);
			_service.addEventListener(FaultEvent.FAULT, _Fault);
			_service.updatePurchaseStatus(id);
		}
		
		private function updatePurchaseStatusResult(e:ResultEvent):void{
			_service.removeEventListener(ResultEvent.RESULT,updatePurchaseStatusResult);
			if(e.result != null && e.result == "success") {
				AppAlert.show("订单状态已更改！","提示");
			    Env.dispatchEvent(new  CustomerEvent("reGetCheckInoutList"));
			 } 
		}
		
		public function purchaseReturnSend(id:Number):void{
			_service.addEventListener(ResultEvent.RESULT,purchaseReturnSendProcess);
			_service.addEventListener(FaultEvent.FAULT, _Fault);
			_service.purchaseReturnCheckOut(id);
		}
		
		private function purchaseReturnSendProcess(e:ResultEvent):void{
			_service.removeEventListener(ResultEvent.RESULT,purchaseReturnSendProcess);
			Env.dispatchEvent(new CustomerEvent("purchaseReturnSendSuccess",e.result));
		}
		
		public function removeCIO(id:Number):void{
			_service.addEventListener(ResultEvent.RESULT,processRemoveCIO);
			_service.addEventListener(FaultEvent.FAULT, _Fault);
			_service.removeCIO(id);
		}
		
		private function processRemoveCIO(e:ResultEvent):void{
			_service.removeEventListener(ResultEvent.RESULT,processRemoveCIO);
			Env.dispatchEvent(new CustomerEvent("processRemoveCIOSuccess",e.result));
		}
	}
}